package com.xywh.dakaqiandao.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywh.dakaqiandao.R;
import com.xywh.dakaqiandao.mvp.ui.entity.DiQuSheZhi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DiQuSheZhiAdapter extends BaseQuickAdapter<DiQuSheZhi, BaseViewHolder> {

    public Map<Integer, Boolean> map = new HashMap<>();
    private Context mContext;
    private DiQuHashMap mDiQuHashMap;


    public DiQuSheZhiAdapter(Context context, @Nullable List<DiQuSheZhi> data, DiQuHashMap diQuHashMap) {
        super(R.layout.item_di_qu_she_zhi, data);
        this.mContext = context;
        this.mDiQuHashMap = diQuHashMap;

    }

    @Override
    protected void convert(BaseViewHolder helper, DiQuSheZhi item) {
        //GlideUtil.loadImage(mContext,item.getImg(),helper.getView(R.id.img_head));
        helper.setText(R.id.tv_name, item.getRegionName());
        helper.setOnCheckedChangeListener(R.id.cb, null);//清掉监听器

        int position = helper.getPosition();
        helper.setChecked(R.id.cb, map.get(position));//设置选中状态
        if (map.get(position) == true) {
            mDiQuHashMap.addHashMapData(item);
        } else {
            mDiQuHashMap.removeHashMapData(item);
        }

        helper.setOnCheckedChangeListener(R.id.cb, new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    mDiQuHashMap.addHashMapData(item);
                } else {
                    mDiQuHashMap.removeHashMapData(item);
                }
                map.put(helper.getPosition(), isChecked);//check状态一旦改变，保存的check值也要

            }
        });


    }

    public interface DiQuHashMap {

        void addHashMapData(DiQuSheZhi diQuSheZhi);

        void removeHashMapData(DiQuSheZhi diQuSheZhi);

    }

}
